package top.xiaoshuaila.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.security.core.context.SecurityContextHolder;
import top.xiaoshuaila.pojo.Admin;
import top.xiaoshuaila.pojo.Menu;
import top.xiaoshuaila.mapper.MenuMapper;
import top.xiaoshuaila.service.IMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import top.xiaoshuaila.util.AdminUtils;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 小帅啦
 * @since 2021-05-01
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements IMenuService {

    @Autowired
    private MenuMapper menuMapper;
    @Autowired
    private RedisTemplate<String,Object>redisTemplate;
    @Override
    public List<Menu> getMenusByAdminId() {
        Integer id =
                AdminUtils.getCurrentAdmin().getId();
        ValueOperations<String,Object>valueOperations=
                redisTemplate.opsForValue();
        //从redis缓存中获取该id的菜单
        List<Menu>menus= (List<Menu>) valueOperations.get("menu_"+id);
        //如果为空,正常走数据库查找,并且放入redis缓存,如果不为空,则直接返回查找到的对象
        if(menus==null){
            menus=menuMapper.getMenusByAdminId(id);
            valueOperations.set("menu_"+id,menus);
        }
        return menus;
    }

    @Override
    public List<Menu> getMenusWithRole() {
        return menuMapper.getMenusWithRole();
    }

    @Override
    public List<Menu> getAllMenus() {
        return menuMapper.getAllMenus();
    }
}
